
section == "MULTIPLEX" {
    # For multiplex select mplexid, frequency, sourceid from dtv_multiplex
    if (($1 + 0) == 0)
        next
    if ($3 != sourceid)
        next
    # mplexid_by_freq indexed by frequency and version
    mplexid_by_freq[$2, ver] = $1
    if (debug)
        print "MULTIPLEX mplexid_by_freq[" $2 "," ver "] = " $1
    # freq_by_mplexid indexed by mplexid and version
    freq_by_mplexid[$1, ver] = $2
}

section == "SCAN" {
    if ($1 == "VC")
        section = "VC"
    if (debug)
        print "SCAN $1=" $1
}

section == "VC" {
    if ($1 == "CD" && $2 == "FREQUENCY") {
        section = "FREQUENCY"
        next
    }
    if (NF < 4)
        next
    if (($1 + 0) == 0)
        next
    split($2, chanpart,".")
    prior_channum = ""
    if ((chanpart[1],chanpart[2]) in scte_channum) {
        if (scte_channum[chanpart[1],chanpart[2]] != $1) {
            prior_channum = scte_channum[chanpart[1],chanpart[2]]
            print "Multiple Channel Numbers for Pchannel " $2 " in download"
            print "scte_channum[" chanpart[1] "," chanpart[2] "]=" prior_channum " and " $1
            prior_channum = prior_channum ","
        }
        if (scte_channame[chanpart[1],chanpart[2]] != $4) {
            print "ERROR Multiple Channel Names for Pchannel " $2 " in download"
            print "scte_channame[" chanpart[1] "," chanpart[2] "]=" scte_channame[chanpart[1],chanpart[2]] " and " $4
            retcode = 2
        }
    }
    # scte_channum indexed by pchannel and serviceid
    scte_channum[chanpart[1],chanpart[2]] = prior_channum $1
    # scte_channame indexed by pchannel and serviceid
    scte_channame[chanpart[1],chanpart[2]] = $4
    if (debug) {
        print "VC scte_channum[" chanpart[1] "," chanpart[2] "]=" prior_channum $1
        print "VC scte_channame[" chanpart[1] "," chanpart[2] "]=" $4
    }
}

section == "FREQUENCY" {
    if ($1 == "M#") {
        section = "MODE"
        next
    }
    if (NF != 2)
        next
    if (($1 + 0) == 0)
        next
    freqval = substr($2,1,length($2)-2)
    if ((freqval + 0) == 0)
        next
    freq_by_pchannel[$1] = freqval
    pchannel_by_freq[freqval] = $1
    if (debug) {
        print "FREQUENCY freq_by_pchannel[" $1 "] = " freq_by_pchannel[$1]
        print "FREQUENCY pchannel_by_freq[" freqval "] = " pchannel_by_freq[freqval]
    }
}

section == "CHANNEL" && ver == 1 {
    # for this section select chanid, channum, freqid, mplexid, serviceid, sourceid, callsign, name, xmltvid, recpriority, visible from channel
    if (($1 + 0) == 0)
        next
    channum = $2
    mplexid = $4
    serviceid = $5
    source = $6
    callsign = $7
    name = $8
    xmltvid = $9
    recpriority = $10
    visible = $11

    if (! (source in sources))
        sources[source] = source
    frequency = freq_by_mplexid[mplexid,ver]
    if (name != "")
        chan_channum_by_name[name,source] = channum
    chan_channum_by_freq_serviceid[frequency,serviceid,source] = channum
    chan_xmltvid_by_channum[channum,source] = xmltvid
    chan_callsign_by_channum[channum,source] = callsign
    chan_freq_by_channum[channum,source] = frequency
    chan_serviceid_by_channum[channum,source] = serviceid
    chan_recpriority_by_channum[channum,source] = recpriority
    chan_visible_by_channum[channum,source] = visible
    if (debug) {
        print "CHANNEL 1 chan_channum_by_name[" name "," source "] = " channum
        print "CHANNEL 1 chan_channum_by_freq_serviceid[" frequency ","  serviceid "," source "] = " channum
        print "CHANNEL 1 chan_xmltvid_by_channum[" channum "," source "] = " xmltvid
        print "CHANNEL 1 chan_callsign_by_channum[" channum "," source "] = " callsign
        print "CHANNEL 1 chan_freq_by_channum[" channum "," source "] = " frequency
        print "CHANNEL 1 chan_serviceid_by_channum[" channum "," source "] = " serviceid
        print "CHANNEL 1 chan_recpriority_by_channum[" channum "," source "] = " recpriority
    }
}

section == "CHANNEL" && ver == 2 {
    # for this section select chanid, channum, freqid, mplexid, serviceid, sourceid, callsign, name, xmltvid, recpriority, visible from channel
    if (($1 + 0) == 0)
        next
    if ($6 != sourceid)
        next
    chanid = $1
    channum = $2
    freqid = $3
    mplexid = $4
    serviceid = $5
    source = $6
    callsign = $7
    name = $8
    xmltvid = $9
    recpriority = $10
    visible = $11

    new_channum = ""
    new_xmltvid = ""
    new_callsign = ""
    new_recpriority = ""
    # We make channels invisible if there is no proper channel number found
    new_visible = 0

    # Here we process the freshly scanned channels.
    #  channum will be wrong
    #  callsign, name may have been changed or cleared
    #  xmltvid may have been cleared

    # Match frequency and serviceid with scte data -
    #  get channum and name -
    #  set channum and put name into callsign if callsign is blank
    frequency = freq_by_mplexid[mplexid, ver]
    pchannel = pchannel_by_freq[frequency]
    if (debug) {
        print "CHANNEL 2 chanid:" chanid ",frequency:" frequency ",pchannel:" pchannel
    }
    if ( (pchannel,serviceid) in scte_channum) {
        new_channum = scte_channum[pchannel, serviceid]
        if (split(new_channum, parts,",") > 1) {
            for (ix in parts) {
                for (sourcex in sources) {
                    if ((parts[ix],sourcex) in chan_freq_by_channum) {
                        print "Selected channel " parts[ix] " out of <" new_channum ">"
                        new_channum = parts[ix]
                        break
                    }
                }
                if (new_channum == parts[ix])
                    break
            }
        }
        new_callsign = scte_channame[pchannel, serviceid]
        new_visible = 1
    }

    # Channels which do not match scte -
    #  Match with channel table before scan on name if there is a name
    #  if found copy channum
    #else if ( (name,source) in chan_channum_by_name) {
    #    new_channum = chan_channum_by_name[name,source]
    #    new_visible = 1
    #}

    # Channel Still not found -
    #  Match with channel table before on freq and serviceid -
    #  As long as it is visible (i.e. has valid frequency etc)
    #  if found copy channum.
    # Dangerous - maybe the old freq and service is now used by another channel ?
    #else if ( (frequency,serviceid,source) in chan_channum_by_freq_serviceid) {
    #    new_channum = chan_channum_by_freq_serviceid[frequency,serviceid,source]
    #    if (new_channum in new_channums || ! chan_visible_by_channum[new_channum,source])
    #        new_channum = ""
    #    else {
    #        print "WARNING possible dubious channel. Num:" new_channum ",Freq:" frequency ",Service:" serviceid ",Name:" name ",Callsign:" callsign ",Channum:" channum
    #        print "If you see a duplicate channel error, blank out channel number " new_channum " in Database before rerunning."
    #        new_visible = 1
    #    }
    #}

    # All - match new channel on old by channum and source
    #  Copy xmltvid, callsign, recpriority if found
    if (new_channum != "" \
        && (new_channum,source) in chan_callsign_by_channum \
        && chan_callsign_by_channum[new_channum,source] != "") {
        new_xmltvid = chan_xmltvid_by_channum[new_channum,source]
        new_callsign = chan_callsign_by_channum[new_channum,source]
        new_recpriority = chan_recpriority_by_channum[new_channum,source]
        old_channel_matched[new_channum] = new_channum
    }

    # If no match in old by channum and source
    #  Try channum and other sources
    #  Copy xmltvid and callsign if found
    else {
        print "New channel found. Num:" new_channum ",Freq:" frequency ",Service:" serviceid ",Name:" name ",Callsign:" callsign ",Channum:" channum
        if (new_channum != "") {
            for (sourcex in sources) {
                if ( (new_channum,sourcex) in chan_callsign_by_channum \
                    && chan_callsign_by_channum[new_channum,sourcex] != "") {
                    new_xmltvid = chan_xmltvid_by_channum[new_channum,sourcex]
                    new_callsign = chan_callsign_by_channum[new_channum,sourcex]
                    new_recpriority = chan_recpriority_by_channum[new_channum,sourcex]
                    break
                }
            }
        }
    }
    if (new_xmltvid == "")
        print "No xmltvid for channel. Num:" channum ",NewNum:" new_channum ",Freq:" frequency ",Service:" serviceid

    if (! new_visible)
        print "Channel marked not visible. Num:"  channum ",NewNum:" new_channum ",Freq:" frequency ",Service:" serviceid

    #keep track of channels found
    if (new_channum != "") {
        if (new_channum in new_channums) {
            print "ERROR duplicate channel numbers assigned:" new_channum
            retcode=2
        }
        else {
            new_channums[new_channum] = new_channum
        }
    }

    # All update channel table by chanid, setting channum, xmltvid, callsign, recpriority
    sep = ""
    sql = "update channel set "
    if (new_channum != "" && new_channum != channum) {
        sql = sql sep " channum = '" new_channum "'"
        sep = ","
    }
    if (new_xmltvid != "" && new_xmltvid != xmltvid) {
        sql = sql sep " xmltvid = '" new_xmltvid "'"
        sep = ","
    }
    if (new_callsign != "" && new_callsign != callsign) {
        sql = sql sep " callsign = '" new_callsign "'"
        sep = ","
    }
    if (new_recpriority != "" && new_recpriority != recpriority) {
        sql = sql sep " recpriority = '" new_recpriority "'"
        sep = ","
    }
    if (new_visible != visible) {
        sql = sql sep " visible = " new_visible
        sep = ","
    }
    sql = sql " where chanid = " chanid ";"
    if (sep == ",")
        print sql > sqloutfile
}

END {
    # Report any channels that were in old channel table but not in new.
    for (combined in chan_xmltvid_by_channum) {
        split(combined, separate, SUBSEP)
        if (! separate[1] in new_channums) {
            print "Channel Lost. Num:" channum ",Freq:" chan_freq_by_channum[channum,sourceid] ",Service:"  chan_serviceid_by_channum[channum,sourceid]
        }
    }
    exit retcode
}
